package utils.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.lanou.horo.myapplication.R;

import java.util.List;

import entity.HostPageEntity;
import tools.ItemOnClickListenerOther;
import volley.VolleySingleton;

/**
 * Created by dllo on 15/11/20.
 */
public class HostPageApter extends RecyclerView.Adapter<HostPageApter.RecylerAdpter> {
    private List<HostPageEntity.ListEntity> list;
    private ItemOnClickListenerOther listenerOther;


    public void setListenert(ItemOnClickListenerOther listenerOther) {
        this.listenerOther = listenerOther;
    }

    public void  addData(List<HostPageEntity.ListEntity> list){
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public RecylerAdpter onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.fragment_page_host_item,null);
        return new RecylerAdpter(view);
    }

    @Override
    public void onBindViewHolder(RecylerAdpter holder, int position) {
        HostPageEntity.ListEntity lis = list.get(position);
        if(list!=null&& list.size()>0){
            holder.page_host_item_title.setText(lis.getTitle());
            holder.page_host_item_nameOne.setText(lis.getList().get(0).getNickname());
            holder.page_host_item_nameTwo.setText(lis.getList().get(1).getNickname());
            holder.page_host_item_nameThere.setText(lis.getList().get(2).getNickname());

            VolleySingleton.getInstance().getBitmap(lis.getList().get(0).getSmallLogo(),
                    holder.page_host_item_imgIvOne);
            VolleySingleton.getInstance().getBitmap(lis.getList().get(1).getSmallLogo(),
                    holder.page_host_item_imgIvTwo);
            VolleySingleton.getInstance().getBitmap(lis.getList().get(2).getSmallLogo(),
                    holder.page_host_item_imgIvThere);

        }
        holder.pos = position;


    }

    @Override
    public int getItemCount() {
        return list != null && list.size()>0 ? list.size() :0;
    }

    class  RecylerAdpter extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView page_host_item_title,page_host_item_more,
                host_fouse_one,host_fouse_two,host_fouse_there,
                page_host_item_nameOne,page_host_item_nameTwo,page_host_item_nameThere;

        private NetworkImageView page_host_item_imgIvOne,page_host_item_imgIvTwo,
                page_host_item_imgIvThere;

        private  int pos;


        public RecylerAdpter(View itemView) {
            super(itemView);

            host_fouse_one = (TextView) itemView.findViewById(R.id.host_fouse_one);
            host_fouse_two = (TextView) itemView.findViewById(R.id.host_fouse_two);
            host_fouse_there = (TextView) itemView.findViewById(R.id.host_fouse_there);

            host_fouse_one.setOnClickListener(this);
            host_fouse_two.setOnClickListener(this);
            host_fouse_there.setOnClickListener(this);

            page_host_item_title = (TextView) itemView.findViewById(R.id.page_host_item_title);
            page_host_item_more= (TextView) itemView.findViewById(R.id.page_host_item_more);
            page_host_item_nameOne= (TextView) itemView.findViewById(R.id.page_host_item_nameOne);
            page_host_item_nameTwo= (TextView) itemView.findViewById(R.id.page_host_item_nameTwo);
            page_host_item_nameThere= (TextView) itemView.findViewById(R.id.page_host_item_nameThere);

            page_host_item_imgIvOne = (NetworkImageView) itemView.findViewById(R.id.page_host_item_imgIvOne);
            page_host_item_imgIvTwo = (NetworkImageView) itemView.findViewById(R.id.page_host_item_imgIvTwo);
            page_host_item_imgIvThere = (NetworkImageView) itemView.findViewById(R.id.page_host_item_imgIvThere);
            page_host_item_imgIvOne.setOnClickListener(this);
            page_host_item_imgIvTwo.setOnClickListener(this);
            page_host_item_imgIvThere.setOnClickListener(this);


        }

        @Override
        public void onClick(View v) {
            if (list!= null&&list.size() > 0){
                HostPageEntity.ListEntity listEn = list.get(pos);
                if (listenerOther != null){
                    switch (v.getId()){
                        case R.id.host_fouse_one:
                            listenerOther.itemListener(listEn,0);
                            break;
                        case  R.id.host_fouse_two:
                            listenerOther.itemListener(listEn,1);
                            break;
                        case R.id.host_fouse_there:
                            listenerOther.itemListener(listEn,2);
                            break;
                        case R.id.page_host_item_imgIvOne:
                            listenerOther.itemListener(listEn,3);
                            break;
                        case R.id.page_host_item_imgIvTwo:
                            listenerOther.itemListener(listEn,4);
                            break;
                        case R.id.page_host_item_imgIvThere:
                            listenerOther.itemListener(listEn,5);
                            break;



                    }
                }
            }

        }
    }
}
